import math
import os
from PIL import Image

BASE_CSS = """\
.icon {
    width: %(image_width)dpx;
    height: %(image_height)dpx;
    vertical-align: text-top;
    margin-right: 5px;
    border: 0;
    outline: 0;
    background: transparent url(%(image_url)s) no-repeat;
    text-indent: -99999px;
    overflow: hidden;
    display: -moz-inline-box;
    display: inline-block;
}
* html .icon { zoom: 1; display: inline; text-indent: 0; line-height: 0; font-size: 0; background-image: url(%(image_url_ie)s); } /* IE 6 only */
*:first-child+html .icon { zoom: 1; display: inline; text-indent: 0; line-height: 0; font-size: 0; } /* IE 7 only */
"""

ICON_CSS = '.icon-%(name)s { background-position: %(x)dpx %(y)dpx; }'


def create_sprite(images, image_dest, css_dest, image_url=None, image_width=16,
    image_height=16, cols=10):
    """
    :param images:
        A list of image paths to be included in the sprite.
    :param image_dest:
        Destination sprite file.
    :param image_dest:
        Destination CSS file.
    :param image_url:
        Path to the image file used in the CSS.
    :param cols:
        Number of image columns in the generated sprite.
    """
    # Leave a blank spot in 0,0
    images.insert(0, None)
    cols = min(cols, len(images))
    rows = int(math.ceil(len(images) * 1.0 / cols))
    sprite = Image.new('RGBA', (image_width * cols, image_height * rows))

    if image_url is None:
        image_url = os.path.basename(image_dest)

    css = [BASE_CSS % {
        'image_width':  image_width,
        'image_height': image_height,
        'image_url':    image_url,
        'image_url_ie': image_url[:-3] + 'gif'}
    ]

    col = 0
    row = 0
    for image in images:
        x = col * image_width
        y = row * image_height

        if image is not None:
            name = os.path.basename(image)[:-4]
            im = Image.open(image)
            sprite.paste(im, (x, y))
            css.append(ICON_CSS % {'name': name, 'x': x * -1, 'y': y * -1})

        col += 1
        if col == cols:
            col = 0
            row += 1

    sprite.save(image_dest)

    if not image_dest.endswith('gif'):
        # Let's save a gif version for IE6
        im = sprite
        # Get the alpha band
        alpha = im.split()[3]
        # Convert the image into P mode but only use 255 colors in the palette
        # out of 256
        im = im.convert('RGB').convert('P', palette=Image.ADAPTIVE, colors=255)
        # Set all pixel values below 128 to 255, and the rest to 0
        mask = Image.eval(alpha, lambda a: 255 if a <=128 else 0)
        # Paste the color of index 255 and use alpha as a mask
        im.paste(255, mask)
        # The transparency index is 255
        im.save(image_dest[:-3] + 'gif', transparency=255)

    css_contents = '\n'.join(css)
    f = open(css_dest, 'w+')
    f.write(css_contents)
    f.close()


led_icons = [
    'accept.png',
    'add.png',
    'alarm.png',
    'anchor.png',
    'application.png',
    'application2.png',
    'application_add.png',
    'application_cascade.png',
    'application_delete.png',
    'application_double.png',
    'application_edit.png',
    'application_error.png',
    'application_form.png',
    'application_get.png',
    'application_go.png',
    'application_home.png',
    'application_key.png',
    'application_lightning.png',
    'application_link.png',
    'application_osx.png',
    'application_osx_terminal.png',
    'application_put.png',
    'application_side_boxes.png',
    'application_side_contract.png',
    'application_side_expand.png',
    'application_side_list.png',
    'application_side_tree.png',
    'application_split.png',
    'application_tile_horizontal.png',
    'application_tile_vertical.png',
    'application_view_columns.png',
    'application_view_detail.png',
    'application_view_gallery.png',
    'application_view_icons.png',
    'application_view_list.png',
    'application_view_tile.png',
    'application_view_xp.png',
    'application_view_xp_terminal.png',
    'arrow_branch.png',
    'arrow_divide.png',
    'arrow_in.png',
    'arrow_inout.png',
    'arrow_join.png',
    'arrow_left.png',
    'arrow_merge.png',
    'arrow_out.png',
    'arrow_redo.png',
    'arrow_refresh.png',
    'arrow_right.png',
    'arrow_undo.png',
    'asterisk_orange.png',
    'attach.png',
    'attach_2.png',
    'award_star_gold.png',
    'bandaid.png',
    'basket.png',
    'bell.png',
    'bin_closed.png',
    'blog.png',
    'blueprint.png',
    'blueprint_horizontal.png',
    'bluetooth.png',
    'bomb.png',
    'book.png',
    'book_addresses.png',
    'book_next.png',
    'book_open.png',
    'book_previous.png',
    'bookmark.png',
    'bookmark_book.png',
    'bookmark_book_open.png',
    'bookmark_document.png',
    'bookmark_folder.png',
    'books.png',
    'box.png',
    'brick.png',
    'bricks.png',
    'briefcase.png',
    'bug.png',
    'buildings.png',
    'bullet_add_1.png',
    'bullet_add_2.png',
    'bullet_key.png',
    'cake.png',
    'calculator.png',
    'calendar_1.png',
    'calendar_2.png',
    'camera.png',
    'cancel.png',
    'car.png',
    'cart.png',
    'cd.png',
    'chart_bar.png',
    'chart_curve.png',
    'chart_line.png',
    'chart_organisation.png',
    'chart_pie.png',
    'clipboard_paste_image.png',
    'clipboard_sign.png',
    'clipboard_text.png',
    'clock.png',
    'cog.png',
    'coins.png',
    'color_swatch_1.png',
    'color_swatch_2.png',
    'comment.png',
    'compass.png',
    'compress.png',
    'computer.png',
    'connect.png',
    'contrast.png',
    'control_eject.png',
    'control_end.png',
    'control_equalizer.png',
    'control_fastforward.png',
    'control_pause.png',
    'control_play.png',
    'control_repeat.png',
    'control_rewind.png',
    'control_start.png',
    'control_stop.png',
    'control_wheel.png',
    'counter.png',
    'counter_count.png',
    'counter_count_up.png',
    'counter_reset.png',
    'counter_stop.png',
    'cross.png',
    'cross_octagon.png',
    'cross_octagon_fram.png',
    'cross_shield.png',
    'cross_shield_2.png',
    'crown.png',
    'crown_bronze.png',
    'crown_silver.png',
    'css.png',
    'cursor.png',
    'cut.png',
    'dashboard.png',
    'data.png',
    'database.png',
    'databases.png',
    'delete.png',
    'delivery.png',
    'desktop.png',
    'desktop_empty.png',
    'direction.png',
    'disconnect.png',
    'disk.png',
    'doc_access.png',
    'doc_break.png',
    'doc_convert.png',
    'doc_excel_csv.png',
    'doc_excel_table.png',
    'doc_film.png',
    'doc_illustrator.png',
    'doc_music.png',
    'doc_music_playlist.png',
    'doc_offlice.png',
    'doc_page.png',
    'doc_page_previous.png',
    'doc_pdf.png',
    'doc_photoshop.png',
    'doc_resize.png',
    'doc_resize_actual.png',
    'doc_shred.png',
    'doc_stand.png',
    'doc_table.png',
    'doc_tag.png',
    'doc_text_image.png',
    'door.png',
    'door_in.png',
    'drawer.png',
    'drink.png',
    'drink_empty.png',
    'drive.png',
    'drive_burn.png',
    'drive_cd.png',
    'drive_cd_empty.png',
    'drive_delete.png',
    'drive_disk.png',
    'drive_error.png',
    'drive_go.png',
    'drive_link.png',
    'drive_network.png',
    'drive_rename.png',
    'dvd.png',
    'email.png',
    'email_open.png',
    'email_open_image.png',
    'emoticon_evilgrin.png',
    'emoticon_grin.png',
    'emoticon_happy.png',
    'emoticon_smile.png',
    'emoticon_surprised.png',
    'emoticon_tongue.png',
    'emoticon_unhappy.png',
    'emoticon_waii.png',
    'emoticon_wink.png',
    'envelope.png',
    'envelope_2.png',
    'error.png',
    'exclamation.png',
    'exclamation_octagon_fram.png',
    'eye.png',
    'feed.png',
    'feed_ballon.png',
    'feed_document.png',
    'female.png',
    'film.png',
    'films.png',
    'find.png',
    'flag_blue.png',
    'folder.png',
    'font.png',
    'funnel.png',
    'grid.png',
    'grid_dot.png',
    'group.png',
    'hammer.png',
    'hammer_screwdriver.png',
    'hand.png',
    'hand_point.png',
    'heart.png',
    'heart_break.png',
    'heart_empty.png',
    'heart_half.png',
    'heart_small.png',
    'help.png',
    'highlighter.png',
    'house.png',
    'html.png',
    'image_1.png',
    'image_2.png',
    'images.png',
    'inbox.png',
    'ipod.png',
    'ipod_cast.png',
    'joystick.png',
    'key.png',
    'keyboard.png',
    'layer_treansparent.png',
    'layers.png',
    'layout.png',
    'layout_header_footer_3.png',
    'layout_header_footer_3_mix.png',
    'layout_join.png',
    'layout_join_vertical.png',
    'layout_select.png',
    'layout_select_content.png',
    'layout_select_footer.png',
    'layout_select_sidebar.png',
    'layout_split.png',
    'layout_split_vertical.png',
    'lifebuoy.png',
    'lightbulb.png',
    'lightbulb_off.png',
    'lightning.png',
    'link.png',
    'link_break.png',
    'lock.png',
    'lock_unlock.png',
    'magnet.png',
    'magnifier.png',
    'magnifier_zoom_in.png',
    'male.png',
    'map.png',
    'marker.png',
    'medal_bronze_1.png',
    'medal_gold_1.png',
    'media_player_small_blue.png',
    'microphone.png',
    'mobile_phone.png',
    'money.png',
    'money_dollar.png',
    'money_euro.png',
    'money_pound.png',
    'money_yen.png',
    'monitor.png',
    'mouse.png',
    'music.png',
    'music_beam.png',
    'neutral.png',
    'new.png',
    'newspaper.png',
    'note.png',
    'nuclear.png',
    'package.png',
    'page.png',
    'page_2.png',
    'page_2_copy.png',
    'page_code.png',
    'page_copy.png',
    'page_excel.png',
    'page_lightning.png',
    'page_paste.png',
    'page_red.png',
    'page_refresh.png',
    'page_save.png',
    'page_white_cplusplus.png',
    'page_white_csharp.png',
    'page_white_cup.png',
    'page_white_database.png',
    'page_white_delete.png',
    'page_white_dvd.png',
    'page_white_edit.png',
    'page_white_error.png',
    'page_white_excel.png',
    'page_white_find.png',
    'page_white_flash.png',
    'page_white_freehand.png',
    'page_white_gear.png',
    'page_white_get.png',
    'page_white_paintbrush.png',
    'page_white_paste.png',
    'page_white_php.png',
    'page_white_picture.png',
    'page_white_powerpoint.png',
    'page_white_put.png',
    'page_white_ruby.png',
    'page_white_stack.png',
    'page_white_star.png',
    'page_white_swoosh.png',
    'page_white_text.png',
    'page_white_text_width.png',
    'page_white_tux.png',
    'page_white_vector.png',
    'page_white_visualstudio.png',
    'page_white_width.png',
    'page_white_word.png',
    'page_white_world.png',
    'page_white_wrench.png',
    'page_white_zip.png',
    'paintbrush.png',
    'paintcan.png',
    'palette.png',
    'paper_bag.png',
    'paste_plain.png',
    'paste_word.png',
    'pencil.png',
    'photo.png',
    'photo_album.png',
    'photos.png',
    'piano.png',
    'picture.png',
    'pilcrow.png',
    'pill.png',
    'pin.png',
    'pipette.png',
    'plaing_card.png',
    'plug.png',
    'plugin.png',
    'printer.png',
    'projection_screen.png',
    'projection_screen_present.png',
    'rainbow.png',
    'report.png',
    'rocket.png',
    'rosette.png',
    'rss.png',
    'ruby.png',
    'ruler_1.png',
    'ruler_2.png',
    'ruler_crop.png',
    'ruler_triangle.png',
    'safe.png',
    'script.png',
    'selection.png',
    'selection_select.png',
    'server.png',
    'shading.png',
    'shape_aling_bottom.png',
    'shape_aling_center.png',
    'shape_aling_left.png',
    'shape_aling_middle.png',
    'shape_aling_right.png',
    'shape_aling_top.png',
    'shape_flip_horizontal.png',
    'shape_flip_vertical.png',
    'shape_group.png',
    'shape_handles.png',
    'shape_move_back.png',
    'shape_move_backwards.png',
    'shape_move_forwards.png',
    'shape_move_front.png',
    'shape_square.png',
    'shield.png',
    'sitemap.png',
    'slide.png',
    'slides.png',
    'slides_stack.png',
    'smiley_confuse.png',
    'smiley_cool.png',
    'smiley_cry.png',
    'smiley_fat.png',
    'smiley_mad.png',
    'smiley_red.png',
    'smiley_roll.png',
    'smiley_slim.png',
    'smiley_yell.png',
    'socket.png',
    'sockets.png',
    'sort.png',
    'sort_alphabet.png',
    'sort_date.png',
    'sort_disable.png',
    'sort_number.png',
    'sort_price.png',
    'sort_quantity.png',
    'sort_rating.png',
    'sound.png',
    'sound_note.png',
    'spellcheck.png',
    'sport_8ball.png',
    'sport_basketball.png',
    'sport_football.png',
    'sport_golf.png',
    'sport_raquet.png',
    'sport_shuttlecock.png',
    'sport_soccer.png',
    'sport_tennis.png',
    'stamp.png',
    'star_1.png',
    'star_2.png',
    'status_online.png',
    'stop.png',
    'style.png',
    'sum.png',
    'sum_2.png',
    'switch.png',
    'tab.png',
    'table.png',
    'tag.png',
    'tag_blue.png',
    'target.png',
    'telephone.png',
    'television.png',
    'text_align_center.png',
    'text_align_justify.png',
    'text_align_left.png',
    'text_align_right.png',
    'text_allcaps.png',
    'text_bold.png',
    'text_columns.png',
    'text_dropcaps.png',
    'text_heading_1.png',
    'text_horizontalrule.png',
    'text_indent.png',
    'text_indent_remove.png',
    'text_italic.png',
    'text_kerning.png',
    'text_letter_omega.png',
    'text_letterspacing.png',
    'text_linespacing.png',
    'text_list_bullets.png',
    'text_list_numbers.png',
    'text_lowercase.png',
    'text_padding_bottom.png',
    'text_padding_left.png',
    'text_padding_right.png',
    'text_padding_top.png',
    'text_signature.png',
    'text_smallcaps.png',
    'text_strikethrough.png',
    'text_subscript.png',
    'textfield.png',
    'textfield_rename.png',
    'ticket.png',
    'timeline_marker.png',
    'traffic.png',
    'transmit.png',
    'trophy.png',
    'trophy_bronze.png',
    'trophy_silver.png',
    'ui_combo_box.png',
    'ui_saccordion.png',
    'ui_slider_1.png',
    'ui_slider_2.png',
    'ui_tab_bottom.png',
    'ui_tab_content.png',
    'ui_tab_disable.png',
    'ui_tab_side.png',
    'ui_text_field_hidden.png',
    'ui_text_field_password.png',
    'umbrella.png',
    'user.png',
    'user_black_female.png',
    'user_business.png',
    'user_business_boss.png',
    'user_female.png',
    'user_silhouette.png',
    'user_thief.png',
    'user_thief_baldie.png',
    'vcard.png',
    'vector.png',
    'wait.png',
    'wall.png',
    'wall_break.png',
    'wall_brick.png',
    'wall_disable.png',
    'wand.png',
    'weather_clouds.png',
    'weather_cloudy.png',
    'weather_lightning.png',
    'weather_rain.png',
    'weather_snow.png',
    'weather_sun.png',
    'webcam.png',
    'world.png',
    'zone.png',
    'zone_money.png',
    'zones.png',
]

curr_dir = os.path.abspath(os.path.dirname(__file__))
base = os.path.join(curr_dir, 'led-icons')

image_dest = os.path.join(curr_dir, 'icons.png')
css_dest = os.path.join(curr_dir, '_icons.css')
# icons = led_icons
icons = [
     'alarm.png',
     'feed.png',
     'folder.png',
     'page.png',
     'page_copy.png',
     'wait.png',
]
images = [os.path.join(base, icon) for icon in icons]

for image in images:
    print os.path.exists(image)

create_sprite(images, image_dest, css_dest, image_url='/static/images/icons.png')
